package com.project.controller;

import com.project.bean.ProductBean;
import com.project.bean.SellCartBean;
import com.project.bean.UserBean;
import com.project.service.ISellCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("sellCart")
@Api(tags = "销售车控制器")
public class SellCartController {
    @Autowired
    private ISellCartService service;

    @RequiresPermissions("sell:out")
    @RequestMapping("findSellCartByUserId")
    @ApiOperation(value = "查看登录用户的销售车", notes = "查看登录用户的销售车")
    public List<SellCartBean> findSellCartByUserId(HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        return service.findSellCartByUserId(user.getUserId());
    }

    @RequiresPermissions("sell:out")
    @RequestMapping("addSellCart")
    @ApiOperation(value = "添加销售商品", notes = "添加销售商品")
    public String addSellCart(ProductBean product, HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        service.addSellCart(user.getUserId(), product);
        return "ok";
    }

    @RequiresPermissions("sell:out")
    @RequestMapping("updateSellNum")
    @ApiOperation(value = "改变销售价", notes = "改变销售价")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sellCartId", value = "销售车id"),
            @ApiImplicitParam(name = "newSellNum", value = "新的销售价")
    })
    public String updateSellNum(Integer sellCartId, Integer newSellNum) {
        service.updateSellNum(sellCartId, newSellNum);
        return "ok";
    }

    @RequiresPermissions("sell:out")
    @RequestMapping("delSellCart")
    @ApiOperation(value = "删除销售车", notes = "删除销售车")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sellCartId", value = "销售车id")
    })
    public String delSellCart(Integer sellCartId) {
        service.delSellCart(sellCartId);
        return "ok";
    }

    @RequiresPermissions("sell:out")
    @RequestMapping("out")
    @ApiOperation(value = "出库", notes = "出库")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "clientName", value = "客户名称"),
            @ApiImplicitParam(name = "sellAddr", value = "送货地址")
    })
    public String out(HttpServletRequest req, String clientName, String sellAddr) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        if (clientName != null && clientName != "" && sellAddr != null && sellAddr != "") {
            service.out(user.getUserId(), clientName, sellAddr);
            return "ok";
        }
        return "no";
    }
}
